package day5.HomeWork;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * ClassName：Test1
 *
 * @Author：小鱼
 * @Create：2025-08-22 16:48
 */
public class Test1 {
    /*
    1.编写两个⽅法，分别实现如下功能：
      (1)save()⽅法：创建三个Person类型对象，将它们使⽤序列化⼿段写⼊本地⽂件中持久化存储
      (2)load()⽅法：将save()⽅法中保存的Person对象读取出来，打印输出属性
    */
    public static void save()throws Exception{
        //创建三个Person类型对象
        Person p1 = new Person("zs",19,"man");
        Person p2 = new Person("ls",20,"man");
        Person p3 = new Person("ww",22,"woman");
        //将它们使⽤序列化⼿段写⼊本地⽂件中持久化存储
        ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream("D:\\IDEA_project\\JP_Java\\week6\\src\\day5\\HomeWork\\1.txt "));
        //写
        oos.writeObject(p1);
        oos.writeObject(p2);
        oos.writeObject(p3);
        System.out.println("写操作成功完成");
        //操作完成后，关闭流
        oos.close();
    }
    public static void load()throws Exception {
        //Person对象读取出来，打印输出属性
        ObjectInputStream ois = new ObjectInputStream(
                new FileInputStream("D:\\IDEA_project\\JP_Java\\week6\\src\\day5\\HomeWork\\1.txt "));
        //读
        Person p1 = (Person) ois.readObject();
        System.out.println("readObject1: " + p1);
        Person p2 = (Person) ois.readObject();
        System.out.println("readObject2: " + p2);
        Person p3 = (Person) ois.readObject();
        System.out.println("readObject3: " + p3);
        //操作完成后，关闭流
        ois.close();

    }

    public static void main(String[] args)throws Exception {
        save();
        load();
    }
}
